package dev.alm.guiguxiaozhiapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import dev.alm.guiguxiaozhiapp.bean.Appointment;
import dev.alm.guiguxiaozhiapp.mapper.AppointmentMapper;
import dev.alm.guiguxiaozhiapp.service.AppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppointmentServiceImpl implements AppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    public Boolean save(Appointment appointment) {
        int insert = appointmentMapper.insert(appointment);
        return insert > 0;
    }

    @Override
    public Appointment findById(Long id) {
        return appointmentMapper.selectById(id);
    }

    @Override
    public Boolean update(Appointment appointment) {
        int i = appointmentMapper.updateById(appointment);
        return i > 0;
    }

    @Override
    public Boolean delete(Long id) {
        int i = appointmentMapper.deleteById(id);
        return i > 0;
    }

    @Override
    public List<Appointment> findAll() {
        List<Appointment> appointments = appointmentMapper.selectList(null);
        return appointments;
    }

    @Override
    public Appointment findOne(Appointment appointment) {
        QueryWrapper<Appointment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", appointment.getUsername());
        queryWrapper.eq("id_card", appointment.getIdCard());
        queryWrapper.eq("department", appointment.getDepartment());
        queryWrapper.eq("date", appointment.getDate());
        queryWrapper.eq("time", appointment.getTime());
        return appointmentMapper.selectOne(queryWrapper);
    }

    @Override
    public List<Appointment> queryByUsername(String username) {
        QueryWrapper<Appointment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return appointmentMapper.selectList(queryWrapper);
    }

}
